package edu.cdnu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.cdnu.common.constant.MessageConstatnt;
import edu.cdnu.common.dto.CatalogueDto;
import edu.cdnu.common.result.ResultEntity;
import edu.cdnu.pojo.CataloguePojo;
import edu.cdnu.service.CatalogueService;

@RestController
@RequestMapping("/catalogue")
public class CatalogueController {
	
	@Autowired
	private CatalogueService catalogueService;
	
	/**
	 * 通过课程id查询目录 ,state为空默认为1-课程章节
	 * @param courseId
	 * @param state 0-禁用，1-课程章节，2-资源目录
	 * @return
	 */
	@GetMapping("/catalogueList/{courseId}")
	public ResultEntity<List<CatalogueDto>>  selectCatalogueDtoListByCourseIdAndState(@PathVariable String courseId,Integer state,Integer studentId){
		return ResultEntity.createSuccessResult(catalogueService.selectCatalogueDtoListByCourseIdAndState(Integer.valueOf(courseId), state));
	}
	
	/**
	 * 新增 或 修改 资源目录
	 * @param cataloguePojo
	 */
	@PostMapping("/addOrUpdateCatalogue")
	public ResultEntity<?> addOrUpdateCatalogue(@RequestBody CataloguePojo cataloguePojo) {
//		if(cataloguePojo.getCourseId() == null || "".equals(cataloguePojo.getCatalogueText())) {
		if("".equals(cataloguePojo.getCatalogueText())) {
			return ResultEntity.createFailResult(MessageConstatnt.PARAMETER_EXCEPTION);
		}
		catalogueService.addOrUpdateCatalogue(cataloguePojo);
		return ResultEntity.createSuccessResult(null);
	}
	
	/*
	 
	 删除接口用的 file/deleteCatalogue/{catalogueId}
	 这里有个尾巴，只删除了当前节点没有判断子节点，先用到，后面优化
	 */
}
